using System;
using System.IO;
using System.Xml.Schema;
using uTILLIty.Rave.Core.StorageAdapter;

namespace uTILLIty.Rave.Core.Schema
{
	/// <summary>
	/// A list of well-known schemas which can be resolved within the IoC container
	/// </summary>
	public class WellKnownSchemas : Contract.Schema.WellKnownSchemas
  {
    /// <summary>
    /// The namespace of the <see cref="NtfsStorageAdapter"/>'s extension to the configuration
    /// </summary>
		public const string RaveNtfsConfiguration = RaveConfiguration + "/ntfs";

		/// <summary>
		/// Retrieves the schema saved as an assembly-ressource
		/// </summary>
		/// <typeparam name="T">The namespace and assembly of the supplied type to look for the schema-resource</typeparam>
		/// <param name="resourceKey">The name of the resource within the namespace and assembly of the provided <typeparamref name="T">type</typeparamref></param>
		/// <returns>A new instance of the schema loaded from the assembies resources</returns>
		public static XmlSchema GetSchemaResource<T>(string resourceKey)
		{
			using (Stream data = typeof(T).Assembly.GetManifestResourceStream(typeof(T).Namespace + "." + resourceKey))
			{
				if (data == null)
					throw new NotSupportedException(string.Format("Cannot locate resource '{0}'", resourceKey));

				var schema = XmlSchema.Read(data, null);
				return schema;
			}
		}
	}
}